<?php
// @formatter:off
/**
 * @file c.repositories.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-08-05 00:26:28 +0000 (Mon, 05 Aug 2013) $
 *
 * $Id: c.repositories.php 100 2013-08-05 00:26:28Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.2/extensions/Repositories/c.repositories.php $
 */
// @formatter:on

global $wcGroup;

$wcThemeAssigns["PATH"] = "Repositories Status";

if($wcProfile->allowedTo(WC_E_REPOSITORIES_ALLOWED)) {
	global $wcDirectoriesHolder;
	global $wctDefaults;

	$repo = WCERepository::Instance($wcGroup);

	$repoDir = $repo->mainDirectory();

	$wcThemeAssigns["repo-bad"] = "";
	$wcThemeAssigns["repo-offset"] = "";
	$wcThemeAssigns["repo-balancer"] = "";
	if($repoDir) {
		$wcThemeAssigns["repo-id"] = $repoDir->id;
		$wcThemeAssigns["repo-path"] = $repoDir->name;
		$wcThemeAssigns["repo-count"] = $repoDir->count == 0 ? "Ok" : $repoDir->count;
		if($repo->broken()) {
			$wcThemeAssigns["repo-status"] = "Broken";
		} elseif($repo->balanced()) {
			$wcThemeAssigns["repo-status"] = "Balanced";
		} else {
			$wcThemeAssigns["repo-unbalanced"] = true;
			$wcThemeAssigns["repo-status"] = "Unbalanced";
			$wcThemeAssigns["repo-bad-id"] = $repo->badDirectory()->id;
			$wcThemeAssigns["repo-bad-name"] = $repo->badDirectory()->name;
			$wcThemeAssigns["repo-offset"] = $repo->badDirectory()->count - $repo->correctionSize();
			if($repo->balancer()) {
				$wcThemeAssigns["repo-balancer-id"] = $repo->balancer()->id;
				$wcThemeAssigns["repo-balancer-name"] = $repo->balancer()->name;
			} else {
				$wcThemeAssigns["repo-balancer-id"] = "";
				$wcThemeAssigns["repo-balancer-name"] = "";
			}
		}
	} else {
		$wcThemeAssigns["repo-id"] = "unknown";
		$wcThemeAssigns["repo-path"] = WC_E_REPOSITORIES_MAINDIR;
		$wcThemeAssigns["repo-count"] = $repoDir->count;
		$wcThemeAssigns["repo-status"] = "Broken";
	}

	$directories = $repo->directories();
	uasort($directories, "WCDirectory::NameSort");

	$list = array();
	foreach($directories as $dir) {
		$aux = array(
			"id" => $dir->id,
			"name" => $dir->name,
			"count" => $dir->count
		);
		if($dir->count == WC_E_REPOSITORIES_MAX) {
			$aux["status"] = "Full";
		} elseif($dir->count > WC_E_REPOSITORIES_MAX) {
			$aux["status"] = "Overload";
		} elseif($dir->count == 0) {
			$aux["status"] = "Free";
		} else {
			$aux["status"] = "Ok";
		}
		$list[] = $aux;
	}

	$wcThemeAssigns["repositories"] = $list;
} else {
	if(!$wcProfile->allowedTo(WC_E_REPOSITORIES_ALLOWED)) {
		$wcThemeAssigns["error-message"] = "You don't have enough priviliges";
		require "{$this->_themeRoot}/controlers/contents/c.403.php";
	} else {
		$wcThemeAssigns["error-message"] = "There seems to be a problem with parameters";
		require "{$this->_themeRoot}/controlers/contents/c.500.php";
	}
}
?>